package ch.escaflowne.mangalist;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu.*;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TwoLineListItem;

public class showMangaInformation extends ListActivity
{
	Manga manga;
	int _id;
	Bundle extras;
	Toast toast;
	Cursor list;
	
	@Override
	 public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.displaymanga);
		
		extras = getIntent().getExtras();
		_id = extras.getInt("MANGA_ID");
		manga = new Manga(this);
		
		drawUI();
		
		ListView lv = getListView();
		registerForContextMenu(lv);
	}
	
	final static int CONTEXT_MENU_DELETE = 0;
	final static int CONTEXT_MENU_EDIT = 1;
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		if (v.getId()==getListView().getId())
		{
			menu.setHeaderTitle("Context menu");
			
			menu.add(Menu.NONE, CONTEXT_MENU_DELETE, 0, "Delete");
			menu.add(Menu.NONE, CONTEXT_MENU_EDIT, 1, "Edit");
		}
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		Cursor c = manga.getVolumes(_id);
		
		if (c.moveToPosition(info.position))
		{
			switch (item.getItemId())
			{
			case CONTEXT_MENU_DELETE:
				String isbn = list.getString(Manga.GET_VOLUMES_ISBN);
				
				manga.removeVolume(isbn);
				
			//	toast = Toast.makeText(getApplicationContext(), "Deleting item #" + info.position + ", ISBN: " + isbn, Toast.LENGTH_SHORT);
			//	toast.show();
				
				drawUI();
				break;
				
			case CONTEXT_MENU_EDIT:
				toast = Toast.makeText(getApplicationContext(), "Function not yet implemented", Toast.LENGTH_SHORT);
				toast.show();
				//TODO: Informationen über einzelnen Band bearbeiten
				break;
			}
		}
		else
		{
			toast = Toast.makeText(getApplicationContext(), "Cannot delete item #" + info.position, Toast.LENGTH_SHORT);
			toast.show();
		}
		return super.onContextItemSelected(item);
	}
	
	private void drawUI()
	{
		Cursor c;
		if (extras != null)
		{
			try{
				c = manga.getReadableDatabase().rawQuery("SELECT title,autor,volumes FROM manga WHERE _id = ?", new String[]{String.valueOf(_id)});
				
				if (c.moveToFirst())
				{
					TextView tv = (TextView)findViewById(R.id.title);
					tv.setText(c.getString(0));
					tv.setTextSize(30);
					tv = (TextView)findViewById(R.id.autor);
					tv.setText(c.getString(1));
					tv = (TextView)findViewById(R.id.volumecount);
					tv.setText(String.valueOf(c.getInt(2)));
				}
			}catch(SQLException e){
				toast = Toast.makeText(getApplicationContext(), "SQL error in rawQuery -> manga", Toast.LENGTH_SHORT);
				toast.show();
			}
			try{
				list = manga.getVolumes(_id);
				
				startManagingCursor(list);
				
				SimpleCursorAdapter sca = new SimpleCursorAdapter(this,
						android.R.layout.two_line_list_item,
						list,
						new String[]{"volume","_id"},
						new int[]{android.R.id.text1, android.R.id.text2}
				);
				setListAdapter(sca);
			}catch(SQLException e)
			{
				toast = Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG);
				toast.show();
				Log.e("MangaListe", e.toString());
			}
		}
	}
	
	final static int ADD_VOLUME = 0;
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	menu.add(0, ADD_VOLUME, Menu.NONE, "Add next volume");
    	
    	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch (item.getItemId())
    	{
    	case ADD_VOLUME:
    		//scanBarcode();
    		Intent intent = new Intent(this, addVolume.class);
    		
    		intent.putExtra(addVolume.INTENT_PARAMETER_VOLUME, manga.getVolumeCount(_id) + 1);
    		intent.putExtra(addVolume.INTENT_PARAMETER_MANGAID, _id);
    		//startActivityForResult(intent, INTENT_NR_ADD_VOLUME);
    		startActivity(intent);
    		
    		
    		break;
    		
    	default:
    	}
    	
		return super.onOptionsItemSelected(item);
    }
    
private static final int INTENT_NR_SCAN_BARCODE	= 0;
private static final int INTENT_NR_ADD_VOLUME	= 1;
    /*
    private void scanBarcode()
    {
		Intent intent = new Intent("com.google.zxing.client.android.SCAN");
		intent.setPackage("com.google.zxing.client.android");
		//intent.putExtra("SCAN_MODE", "ONE_D_MODE");
		startActivityForResult(intent, INTENT_NR_SCAN_BARCODE);
    }
    */

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		switch (requestCode)
		{
		/*
		case INTENT_NR_SCAN_BARCODE:
			switch (resultCode)
			{
			case RESULT_OK:
				String isbn = intent.getStringExtra("SCAN_RESULT");
				//String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
				// Handle successful scan
				
				manga.addVolume(isbn, _id, manga.getReadableDatabase().rawQuery("SELECT * FROM volumes WHERE _id = ?", new String[]{String.valueOf(_id)}).getCount() + 1);
				
				toast = Toast.makeText(getApplicationContext(), "Scan erfolgreich: ISBN = " + isbn, Toast.LENGTH_SHORT);
				toast.show();
				break;
				
			case RESULT_CANCELED:
				// Handle cancel
				toast = Toast.makeText(getApplicationContext(), "Scan abgebrochen", Toast.LENGTH_SHORT);
				toast.show();
				break;
			}
			break;
			*/
		
			/*
		case INTENT_NR_ADD_VOLUME:
			if (resultCode == RESULT_CANCELED)
				break;
			if (!manga.addVolume(intent.getStringExtra(addVolume.INTENT_PARAMETER_ISBN), _id, intent.getIntExtra(addVolume.INTENT_PARAMETER_VOLUME, -1)))
			{
				int id = manga.findMangabyISBN(intent.getStringExtra(addVolume.INTENT_PARAMETER_ISBN));
				if (id != -1)
					toast = Toast.makeText(getApplicationContext(), "ISBN already in database!", Toast.LENGTH_SHORT);
				else
					toast = Toast.makeText(getApplicationContext(), "Could not add volume to database.", Toast.LENGTH_SHORT);
				toast.show();
			}
			drawUI();
			break;
			*/
			/*
		case IntentIntegrator.REQUEST_CODE:
			IntentResult ir = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
			break;
			*/
		}
	}
}
